/*
	       File: ucFileType.cs
	Create Date: 2007-08-16
	    Version:

	Description:


	 Change log:


*/
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

using GuerillaProgrammer.FileProcessor.Settings;

namespace GuerillaProgrammer.FileProcessor
{
    public partial class ucFileType : UserControl
    {
        #region Private member fields
        private Configuration _Configuration;
        private ConfigurationFileType _FileType;
        #endregion

        #region Constructors and cleanup methods
        public ucFileType()
        {
            InitializeComponent();
        }
        #endregion

        #region Private implementation methods
        private void OnUpdateFileType()
        {
            if (null != UpdateFileType)
            {
                UpdateFileType(_FileType);
            }
        }
        private void UpdateDirectoryProperties(List<Property> properties)
        {
            _FileType.Parameters = new List<ConfigurationFileTypeParameter>();
            foreach (Property property in properties)
            {
                ConfigurationFileTypeParameter parameter = new ConfigurationFileTypeParameter();
                parameter.Name = property.Name;
                parameter.Value = property.Value;
                _FileType.Parameters.Add(parameter);
            }
        }
        private void LoadWorkflowList()
        {
            if(null != _Configuration.Workflows)
            {
                foreach (ConfigurationWorkflow workflow in _Configuration.Workflows)
                {
                    if (null != workflow)
                    {
                        cbWorkflow.Items.Add(workflow);
                    }
                }
            }
            cbWorkflow.DisplayMember = "Name";

        }
        private void SelectWorkflow(string WorkflowID)
        {
            foreach (ConfigurationWorkflow workflow in _Configuration.Workflows)
            {
                if (null != workflow)
                {
                    if (WorkflowID == workflow.ID)
                    {
                        cbWorkflow.SelectedItem = workflow;
                    }
                }
            }
        }
        private string GetSelectedWorkflowID()
        {
            ConfigurationWorkflow workFlow = (ConfigurationWorkflow) cbWorkflow.SelectedItem;

            if (null == workFlow)
            {
                return string.Empty;
            }

            return workFlow.ID;            
        }
        private void OnFinishedUpdate()
        {
            if (null != FinishedUpdate)
            {
                FinishedUpdate(_FileType.Name, _FileType.ID);
            }
        }
        #endregion

        #region Events handlers
        private void btnProperties_Click(object sender, EventArgs e)
        {
            frmProperties propertiesForm = new frmProperties();
            propertiesForm.LoadFileProperties(_FileType.Parameters);
            propertiesForm.UpdateProperties += new frmProperties.UpdatePropertiesDelegate(this.UpdateDirectoryProperties);
            propertiesForm.ShowDialog();

        }
        private void btnUpdate_Click(object sender, EventArgs e)
        {
            _FileType.Name = txtName.Text;
            _FileType.Description = txtDescription.Text;
            _FileType.FilePattern = txtFilePattern.Text;
            _FileType.WorkflowID = GetSelectedWorkflowID();
            
            OnUpdateFileType();
            OnFinishedUpdate();
        }
        #endregion

        #region Public interface
        public void LoadFileType(Configuration configuration, ConfigurationFileType fileType)
        {
            if (null != configuration && null != fileType)
            {
                _Configuration = configuration;
                _FileType = fileType;

                txtName.Text = _FileType.Name;
                txtFilePattern.Text = _FileType.FilePattern;
                txtDescription.Text = _FileType.Description;
                LoadWorkflowList();
                SelectWorkflow(_FileType.WorkflowID);
            }

        }

        public delegate void UpdateFileTypeDelegate(ConfigurationFileType fileType);
        public event UpdateFileTypeDelegate UpdateFileType;
        public delegate void FinishedUpdateDelegate(string name, string id);
        public event FinishedUpdateDelegate FinishedUpdate;
        #endregion
    }
}
